package com.bsh.api;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bsh.api.params.DtoMerchantInfo;
import com.bsh.api.params.DtoQueryMerchantInfo;
import com.bsh.api.params.DtoShop;
import com.bsh.service.AccountService;
import com.bsh.service.MerchantCategoryService;
import com.bsh.service.MerchantImageService;
import com.bsh.service.MerchantService;
import com.bsh.service.SmsService;
import com.bsh.service.UserService;
import com.bsh.utils.ApiMessage;
import com.bsh.utils.Des3;
import com.bsh.utils.Pager;

/**
 * 商铺相关 api
 * 
 * @author jane
 * @since 0.0.1
 * 
 */
@Controller
@RequestMapping(value = "/api/v1/shop")
public class ApiShopController {
	private static Logger log = LoggerFactory
			.getLogger(ApiShopController.class);
	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;
	@Autowired
	HttpSession session;
	@Autowired
	UserService servUser;
	@Autowired
	SmsService servSms;
	@Autowired
	AccountService accountServ;
	@Autowired
	MerchantService mercService;
	@Autowired
	MerchantCategoryService merchantCateServ;
	@Autowired
	MerchantImageService merchantImageService;

	/**
	 * 商户列表
	 * 
	 * @param paramStr
	 * @return
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage shopList(
			@RequestParam(value = "paramStr", required = true) String paramStr) {
		ApiMessage msg = null;
		JSONObject dto = (JSONObject) JSON.parse(paramStr);

		Integer pageIndex = dto.getInteger("pageIndex") == null ? 1: dto.getInteger("pageIndex");
		Integer pageSize =  dto.getInteger("pageSize") == null ? 10: dto.getInteger("pageSize");
		Pager pager = new Pager(pageIndex, pageSize);
		DtoShop queryDto=new DtoShop();
		queryDto.setCategory(dto.getInteger("category"));
		queryDto.setCity(dto.getInteger("city"));
		queryDto.setPage(dto.getInteger("page"));
		queryDto.setKeyword(dto.getString("keyword"));
		List<Map<String, Object>> list = mercService.apiGetShopList(pager, queryDto);
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> nM = list.get(i);
			nM.put("imgCover",ApiUtils.imageUrl((String) nM.get("imgCover"), request));
			nM.put("imgCoverSmall", ApiUtils.imageUrl((String) nM.get("imgCoverSmall"), request));
			nM.put("showImg1",ApiUtils.imageUrl((String) nM.get("showImg1"), request));
			nM.put("showImg2",ApiUtils.imageUrl((String) nM.get("showImg2"), request));
		}

		msg = new ApiMessage("200", "商铺列表");
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("shop_list", list);
		data.put("count", pager.getRowCount());
		data.put("total_page", pager.getPageCount());
		data.put("current_page", pageIndex);
		msg.setData(Des3.encResponse(data));

		return msg;
	}

	
	/**
	 * 热门商户列表
	 * 
	 * @param paramStr
	 * @return
	 */
	@RequestMapping(value = "/hotList")
	@ResponseBody
	public ApiMessage hotList() {
		ApiMessage msg = null;
		List<Map<String, Object>> list = mercService.apiGetHotShopList();
		if (ApiUtils.objIsNotEmpty(list)) {
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> nM = list.get(i);
				nM.put("imgCover", ApiUtils.imageUrl((String) nM.get("imgCover"), request));
				nM.put("imgCoverSmall", ApiUtils.imageUrl((String) nM.get("imgCoverSmall"), request));
				nM.put("showImg1", ApiUtils.imageUrl((String) nM.get("showImg1"), request));
				nM.put("showImg2", ApiUtils.imageUrl((String) nM.get("showImg2"), request));
			}
			msg = new ApiMessage("200", "热门商铺列表");
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("hot_list", list);
			msg.setData(Des3.encResponse(data));
		}else{
			msg = new ApiMessage("400", "暂无热门商铺");
		}
		
		return msg;
	}
	
	/**
	 * 商铺详情
	 * 
	 * @param paramStr
	 * @return
	 */
	@RequestMapping(value = "/detail", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage shopDetail(
			@RequestParam(value = "paramStr", required = true) String paramStr) {

		ApiMessage msg = null;
		DtoQueryMerchantInfo dto = Des3.decRequest(paramStr,
				DtoQueryMerchantInfo.class);

		if ("null".equals(String.valueOf(dto.getMerchantId()))
				|| "0".equals(String.valueOf(dto.getMerchantId()))) {
			new ApiMessage("105", "缺少商户id");
			return msg;
		}
		List<DtoMerchantInfo> list = mercService.getMerchantDetailById(dto
				.getMerchantId());
		msg = new ApiMessage("200", "查询成功");
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("token", dto.getToken());
		data.put("merchant_detail", list);
		msg.setData(Des3.encResponse(data));
		return msg;

	}

}
